DWSCAUTH ;CLD/JOLLIS - PREFINITI AUTHENTICATION ROUTINES;9/28/10;src/secu/DWSCAUTH.m
;;3.0;SECU;9/28/10;1
;; $Id: DWSCAUTH.m 4 2010-10-24 17:30:25Z jollis $

;;PDOC
;;SUMMARY Register with authentication
;;DEFFNC
LOGIN(EMAIL,PASSWORD,MODE,SESSID)
;;ARG 1 EMAIL The user's e-mail address
;;ARG 2 PASSWORD The user's password
;;ARG 3 MODE EWD or DIRECT
;;ARG 4 SESSID The EWD session ID if MODE=EWD
;;PDOC/
 S AUTHOK=0
 S EMAILCF="$USER("_EMAIL_").EMAIL"
 S PWCF="$USER("_EMAIL_").PASSWORD"
 S UNAME=$$CFGET^DWORREC(EMAILCF)
 S PW=$$CFGET^DWORREC(PWCF)
 I (EMAIL=UNAME)&(PASSWORD=PW) S AUTHOK=1
 I AUTHOK=1 D
 .I MODE="DIRECT" S ^DWAUTH($J)=EMAIL
 .I MODE="EWD" S ^DWAUTH(SESSID)=EMAIL
 .S CFAC="$USER("_EMAIL_").ACCCODE"
 .S CFVC="$USER("_EMAIL_").VERCODE"
 .S KSTR=MODE_":"_EMAIL
 .S ^DWPRVDOM(KSTR,"ACCODE")=$$CFGET^DWORREC(CFAC)
 .S ^DWPRVDOM(KSTR,"VERCODE")=$$CFGET^DWORREC(CFVC)
 .S ^DWPRVDOM(KSTR,"INTIME")=$H
 .S ^DWPRVDOM(KSTR,"JOB")=$J
 .S ^DWPRVDOM(KSTR,"DEVICE")=$P
 .S ^DWPRVDOM(KSTR,"OUTTIME")=""
 .S ^DWPRVDOM(KSTR,"VISTANAME")=""
 .S ^DWPRVDOM(KSTR,"VISTADUZ")=""
 .S ^DWPRVDOM(KSTR,"VISTANICK")=""
 .N X S X=^DWPRVDOM(KSTR,"ACCODE")
 .I X'="" D
 ..D ^XUSHSH S DUZ=$O(^VA(200,"A",X,0))
 ..N X S X=^DWPRVDOM(KSTR,"VERCODE")
 ..I DUZ'="" D
 ...D ^XUSHSH
 ...N VERIFY S VERIFY=$P(^VA(200,DUZ,.1),"^",2)
 ...I X=VERIFY D
 ....S ^DWPRVDOM(KSTR,"VISTANAME")=$P(^VA(200,DUZ,0),"^",1)
 ....S ^DWPRVDOM(KSTR,"VISTADUZ")=DUZ
 ....S ^DWPRVDOM(KSTR,"VISTANICK")=$P(^VA(200,DUZ,.1),"^",4)
 Q AUTHOK

LOGOUT(MODE,SESSID)
 I MODE="DIRECT" D
 .S PRVNOD=MODE_":"_^DWAUTH($J)
 I MODE="EWD" D
 .S PRVNOD=MODE_":"_^DWAUTH(SESSID)
 S ^DWPRVDOM(PRVNOD,"OUTTIME")=$H
 I SESSID'="" K ^DWAUTH(SESSID)
 I SESSID="" K ^DWAUTH($J)
 Q
 